package com.drought.rained.ui.activity.mine;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.drought.rained.R;
import com.drought.rained.adapter.HelpCenterAdapterLJ;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.HelpCenterDataLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.HelpMvpViewLJ;
import com.drought.rained.presenter.HelpPresenterLJ;
import com.drought.rained.response.HelpCenterResponseLJ;
import com.drought.rained.utils.ToastUtilsLJ;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "帮助中心", path = RouterMapLJ.HELPCENTER_PAGE)
public class HelpCenterActivityLJ extends BaseTitleActivity implements HelpMvpViewLJ {

    @BindView(R.id.layout_data_empty)
    LinearLayout layoutDataEmpty;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.tv_service_tel)
    TextView tvServiceTel;
    private HelpCenterAdapterLJ reAdapter;
    private HelpPresenterLJ helpPresenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        helpPresenter = new HelpPresenterLJ(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider_help_center_lj));
        rv.addItemDecoration(divider);
        reAdapter = new HelpCenterAdapterLJ(this);
        rv.setAdapter(reAdapter);

        helpPresenter.getHelpCenterData();
        showLoadingView();

    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_help_center_lj;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.help_center_page_title);
    }

    @Override
    public void onGetDataNext(HelpCenterResponseLJ helpCenterResponse) {
        dismissLoadingView();
        if (helpCenterResponse.getStatus() == ConstantLJ.HTTP_OK) {
            if (helpCenterResponse.hasData()) {
                layoutDataEmpty.setVisibility(View.GONE);
                fillView(helpCenterResponse.getData());
            } else {
                layoutDataEmpty.setVisibility(View.VISIBLE);
            }
        } else {
            ToastUtilsLJ.showShort(HelpCenterActivityLJ.this, helpCenterResponse.getMessage());
        }
    }

    private void fillView(ArrayList<HelpCenterDataLJ> data) {
        List<HelpCenterDataLJ> list = new ArrayList<>();
        // TODO: 3是客服电话 4是邮箱
        for (int i = 0; i < data.size(); i++) {
            HelpCenterDataLJ helpData = data.get(i);
            if ("3".equals(helpData.getStatus())) {
                tvServiceTel.setText(helpData.getMessage());
            } else if ("4".equals(helpData.getStatus())) {
                tvEmail.setText(helpData.getMessage());
            } else {
                list.add(helpData);
            }
        }

        reAdapter.updateItems(list);
    }

   @OnClick({R.id.tv_service_tel, R.id.tv_email})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_service_tel:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tvServiceTel.getText().toString());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.tv_email:
                // 得到剪贴板管理器
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setPrimaryClip(ClipData.newPlainText("text", tvEmail.getText().toString()));
                if (cm.hasPrimaryClip()) {
                    cm.getPrimaryClip().getItemAt(0).getText();
                }
                ToastUtilsLJ.showShort(HelpCenterActivityLJ.this, getStringResource(R.string.help_center_copy_succeed));
                break;
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
